import 'package:detect/entity/entity.dart';
import 'package:detect/utils/api_util.dart';
import 'package:detect/utils/db_util.dart';
import 'package:detect/utils/option_util.dart';
import 'package:detect/utils/storage_util.dart';
import 'package:intl/intl.dart';

class DetectUtil {
  static DetectUtil? _instance;

  static DetectUtil get instance => _instance ??= DetectUtil._();

  DetectTaskEntity? get detecting {
    Map<String, dynamic>? map = StorageUtil.get<Map<String, dynamic>>("DetectingTask");
    return map == null ? null : DetectTaskEntity.fromJson(map);
  }

  set detecting(DetectTaskEntity? entity) {
    if (entity == null) {
      StorageUtil.del("DetectingTask");
    } else {
      StorageUtil.set<Map<String, dynamic>>("DetectingTask", entity.toJson());
    }
  }

  DetectUtil._();

  Future<void> start(DetectTaskEntity entity) async {
    if (entity.id == null) return;
    String type, csv;
    if (entity.type == TaskType.general.name) {
      type = "continue";
      List<GeneralTaskHoursEntity> generalTaskHoursEntities = await DBApi.generalTaskHours.list("`detect_task_id` = ?", [entity.id]);
      csv = generalTaskHoursEntities.map<String>((e) {
        DateFormat dateFormat = DateFormat("HH:mm:ss");
        DateTime startHour = DateTime.now().copyWith(hour: e.hour, minute: 0, second: 0), endHour = startHour.copyWith(minute: 59, second: 59);
        int index = generalTaskHoursEntities.indexOf(e), startHz = (e.startHz).round(), endHz = (e.endHz).round();
        return "$index,${dateFormat.format(startHour)},${dateFormat.format(endHour)},${e.power},$startHz,$endHz";
      }).join("\n");
      // csv = """
      // 0,00:00:00,00:59:59,0,2000000,5000000
      // 1,01:00:00,01:59:59,0,2000000,5000000
      // 2,02:00:00,02:59:59,0,2000000,5000000
      // 3,03:00:00,03:59:59,0,2000000,5000000
      // 4,04:00:00,04:59:59,0,2000000,5000000
      // 5,05:00:00,05:59:59,0,2000000,5000000
      // 6,06:00:00,06:59:59,0,3000000,6000000
      // 7,07:00:00,07:59:59,0,3000000,6000000
      // 8,08:00:00,08:59:59,0,4000000,7000000
      // 9,09:00:00,09:59:59,0,4000000,7000000
      // 10,10:00:00,10:59:59,0,5000000,8000000
      // 11,11:00:00,11:59:59,0,5000000,8000000
      // 12,12:00:00,12:59:59,0,5000000,8000000
      // 13,13:00:00,13:59:59,0,5000000,8000000
      // 14,14:00:00,14:59:59,0,5000000,8000000
      // 15,15:00:00,15:59:59,0,5000000,8000000
      // 16,16:00:00,16:59:59,0,5000000,8000000
      // 17,17:00:00,17:59:59,0,5000000,8000000
      // 18,18:00:00,18:59:59,0,4000000,7000000
      // 19,19:00:00,19:59:59,0,4000000,7000000
      // 20,20:00:00,20:59:59,0,4000000,7000000
      // 21,21:00:00,21:59:59,0,3000000,6000000
      // 22,22:00:00,22:59:59,0,2000000,5000000
      // 23,23:00:00,23:59:59,0,2000000,5000000
      // """;
    } else {
      type = "selected";
      List<PickTaskHoursEntity> pickTaskHoursEntities = await DBApi.pickTaskHours.list("`detect_task_id` = ?", [entity.id]);
      csv = pickTaskHoursEntities.map<String>((e) {
        DateFormat dateFormat = DateFormat("HH:mm:ss");
        DateTime startHour = DateTime.now().copyWith(hour: e.hour, minute: 0, second: 0), endHour = startHour.copyWith(minute: 59, second: 59);
        int index = pickTaskHoursEntities.indexOf(e);
        return "$index,${dateFormat.format(startHour)},${dateFormat.format(endHour)},${e.power},${e.modemType},${e.strategy},${e.hzs}";
      }).join("\n");
      // csv = """
      // 0,0:00:00,0:59:59,0,c0,once,4099500,4111500,4156500,4165500,4168500,4177500,4225500,4243500,4246500,4255500
      // 1,1:00:00,1:59:59,0,c0,once,5812500,5815500,5824500,5827500,5863500,5902500,5905500,5917500,5926500,5977500
      // 2,2:00:00,2:59:59,0,c0,once,5002500,5005500,5002500,5038500,5053500,5062500,5074500,5086500,5092500,5095500
      // 3,3:00:00,3:59:59,0,c0,once,5002500,5005500,5026500,5029500,5077500,5086500,5104500,5107500,5116500,5131500
      // 4,4:00:00,4:59:59,0,c0,once,5002500,5014500,5026500,5041500,5074500,5086500,5116500,5140500,5176500,5182500
      // 5,5:00:00,5:59:59,0,c0,once,5902500,5905500,5914500,5926500,5974500,5980500,5992500,5995500,6004500,6040500
      // 6,6:00:00,6:59:59,0,c0,once,5002500,5005500,5017500,5020500,5038500,5017500,5053500,5062500,5086500,5092500
      // 7,7:00:00,7:59:59,0,c0,once,5002500,5005500,5017500,5026500,5032500,5038500,5089500,5092500,5104500,5128500
      // 8,8:00:00,8:59:59,0,c0,once,5002500,5038500,5050500,5062500,5074500,5086500,5062500,5095500,5104500,5116500
      // 9,9:00:00,9:59:59,0,c0,once,4099500,4111500,4156500,4165500,4168500,4177500,4225500,4243500,4246500,4255500
      // 10,10:00:00,10:59:59,0,c0,once,5812500,5815500,5824500,5827500,5863500,5902500,5905500,5917500,5926500,5977500
      // 11,11:00:00,11:59:59,0,c0,once,5002500,5005500,5002500,5038500,5053500,5062500,5074500,5086500,5092500,5095500
      // 12,12:00:00,12:59:59,0,c0,once,5002500,5005500,5026500,5029500,5077500,5086500,5104500,5107500,5116500,5131500
      // 13,13:00:00,13:59:59,0,c0,once,5002500,5014500,5026500,5041500,5074500,5086500,5116500,5140500,5176500,5182500
      // 14,14:00:00,14:59:59,0,c0,once,5902500,5905500,5914500,5926500,5974500,5980500,5992500,5995500,6004500,6040500
      // 15,15:00:00,15:59:59,0,c0,once,5002500,5005500,5017500,5020500,5038500,5017500,5053500,5062500,5086500,5092500
      // 16,16:00:00,16:59:59,0,c0,once,5002500,5005500,5017500,5026500,5032500,5038500,5089500,5092500,5104500,5128500
      // 17,17:00:00,17:59:59,0,c0,once,5002500,5038500,5050500,5062500,5074500,5086500,5062500,5095500,5104500,5116500
      // 18,18:00:00,18:59:59,0,c0,once,4012500,4060500,4063500,4072500,4090500,4093500,4102500,4105500,4114500,4117500
      // 19,19:00:00,19:59:59,0,c0,once,5902500,5905500,5914500,5926500,5974500,5980500,5992500,5995500,6004500,6040500
      // 20,20:00:00,20:59:59,0,c0,once,5002500,5005500,5017500,5020500,5038500,5017500,5053500,5062500,5086500,5092500
      // 21,21:00:00,21:59:59,0,c0,once,5002500,5005500,5017500,5026500,5032500,5038500,5089500,5092500,5104500,5128500
      // 22,22:00:00,22:59:59,0,c0,once,5002500,5038500,5050500,5062500,5074500,5086500,5062500,5095500,5104500,5116500
      // 23,23:00:00,23:59:59,0,c0,once,4012500,4060500,4063500,4072500,4090500,4093500,4102500,4105500,4114500,4117500
      // """;
    }
    await ApiUtil().setFreqs(type, csv);
    if (entity.disabledMHzValues?.isNotEmpty ?? false) await ApiUtil().setFreqs("disable", entity.disabledMHzValues?.split(",").map((i) => (double.parse(i) * 1000000).round()).join(","));
    await ApiUtil().startDetect(type, entity.role, entity.radioId);
    await DBApi.detectTask.state(entity.id!, 1);
    detecting = entity;
  }

  Future<void> stop() async {
    if (detecting == null) return;
    await ApiUtil().stopDetect();
    await ApiUtil().setFreqs("disable");
    await ApiUtil().setFreqs(detecting?.type == TaskType.general.name ? "continue" : "selected");
    detecting = null;
  }

  bool isDetecting([int? detectTaskId]) => detectTaskId == null || detectTaskId <= 0 ? detecting?.id != null : detecting?.id == detectTaskId;
}
